package com.jn.util.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.jn.util.CodeUtil;

public class DictCodeRadioGroupTag extends BodyTagSupport {

	private static final long serialVersionUID = -7146553047338820754L;
	private String codeType;
	private String clazz;
	private String style;
	private String name;
	private String value;
	private String ignores;
	private String dire;
	private String defaultValue = "";

	StringBuffer html = new StringBuffer();

	public int doStartTag() throws JspException {
		StringBuffer html = new StringBuffer();
		html.append(getRadios());
		write(this.pageContext, html.toString());
		return SKIP_BODY;
	}

	@Override
	public void release() {
		super.release();
		this.codeType = null;
		this.value = null;
		this.ignores = null;
		this.defaultValue = null;
	}

	private String getRadios() {
		if (this.value == null)
			this.value = "";
		StringBuffer optionBuffer = new StringBuffer();
		Map<String, String> map = CodeUtil.getCodeMap(this.codeType);
		List<String> ignoreList = new ArrayList<String>();
		if (this.ignores != null) {
			String[] arr = this.ignores.split(",");
			for (String s : arr) {
				ignoreList.add(s);
			}
		}
		if (this.dire == null) {
			
		} else if (this.dire.equals("horizontal")) {
			// 水平
			optionBuffer.append("<div class=\"radio\">");
			for (String key : map.keySet()) {
				if (ignoreList.contains(key))
					continue;
				optionBuffer.append("<label><input type=\"radio\" value=\"")
						.append(key).append("\"");
				optionBuffer.append(" name=\"").append(this.name).append("\"");
				if (this.value.equals(key) || this.defaultValue.equals(key))
					optionBuffer.append(" checked=\"checked\"");
				if (this.clazz != null)
					optionBuffer.append(" class=\"").append(this.clazz)
							.append("\"");
				if (this.style != null)
					optionBuffer.append(" style=\"").append(this.style)
							.append("\"");
				optionBuffer.append("/><span class=\"lbl\">")
						.append((String) map.get(key))
						.append("</span></label>");
			}
			optionBuffer.append("</div>");
		} else if (this.dire.equals("vertical")) {
			// 垂直
			for (String key : map.keySet()) {
				if (ignoreList.contains(key))
					continue;
				optionBuffer.append("<div class=\"radio\">");
				optionBuffer.append("<label><input type=\"radio\" value=\"")
						.append(key).append("\"");
				optionBuffer.append(" name=\"").append(this.name).append("\"");
				if (this.value.equals(key) || this.defaultValue.equals(key))
					optionBuffer.append(" checked=\"checked\"");
				if (this.clazz != null)
					optionBuffer.append(" class=\"").append(this.clazz)
							.append("\"");
				if (this.style != null)
					optionBuffer.append(" style=\"").append(this.style)
							.append("\"");
				optionBuffer.append("/><span class=\"lbl\">")
						.append((String) map.get(key))
						.append("</span></label>");
				optionBuffer.append("</div>");
			}
		}
		return optionBuffer.toString();
	}

	public void write(PageContext context, String html) throws JspException {
		JspWriter writer = context.getOut();
		try {
			writer.print(html);
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException(e);
		}
	}

	public String getCodeType() {
		return codeType;
	}

	public void setCodeType(String codeType) {
		this.codeType = codeType;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getIgnores() {
		return ignores;
	}

	public void setIgnores(String ignores) {
		this.ignores = ignores;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getDire() {
		return dire;
	}

	public void setDire(String dire) {
		this.dire = dire;
	}

}
